<?php

namespace App\Http\Controllers\Manager;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Manager;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;

class LoginController extends Controller
{

    public function index()
    {
        $auth = Auth::guard('web')->user();
        return view('login',compact('auth'));
    }

    /**
     * 登陆响应
     */
    public function login(Request $request)
    {
        // 登录验证规则
        $this->validate($request, Manager::rules());
        $name = $request->get('name');
        $password = $request->get('password');
        $user = Manager::where('name',$name)->first();

        $check = ( $user->password === $password);

        if ($check)
        {
            Auth::guard('web')->login($user);
            return Redirect::to('managers/welcome');
        }
        else {
            return Redirect::route('login')
                ->withInput()
                ->withErrors('账号或者密码不正确,请重试！');
        }
    }


    /**
     * 管理员登出
     */
    public function logout()
    {
        if (Auth::guard('web')->check()) {
            Auth::guard('web')->logout();
        }
        return Redirect::route('login');
    }
}